<?php
/**
 *  A List of typed items, with sparse numeric indices
 *
 */
class Yz_List
    extends Yz_Class
    implements Countable
{

    /** The template object/class, subclass of which all list's entries must be
     *  @var Yz_Class
     */
    protected $object = 'Yz_Class';


    /**
     *   @param object|string class
     */
    public function __construct( $class )
    {
        $this->object = $class;
    } // __construct



    /** toString()
    *
    *   @return string
    */
    public function __toString()
    {
        $cl = $this->object;
        $count = $this->count();
        return "List({$cl})({$count})";
    }


    /** isEmpty()
    *   @return bool
    */
    public function isEmpty()
    {
        return ! $this->__dict__;
    } // isEmpty


    /**
    *  Countable method
    *   @return int
    */
    public function count()
    {
        return count( $this->__dict__ );
    }

    public function contains($entry)
    {
        return false;
    }

    public function add($entry)
    {
        return $this;
    }

    public function remove($entry)
    {
        return $this;
    }

    public function clear()
    {
        $this->__dict__ = array();
        return $this;
    }


    /** validate the index, adjust it if required/possible
     *   @param mixed index
     *   @return mixed valid index
     *   @throw Yz_Exception_InvalidIndex if index not valid
     */
    protected function adjustIndex( $index )
    {
        try {
            return Yz_Int::testNotNegative( $index );
        } catch (Yz_Exception_InvalidArgument $ex) {
            throw Yz_Exception_InvalidIndex::_new(array(
                'object' => $this->__toString(),
                'expect' => 'non-empty string',
            ));
        }
    }

    /** the default setter is called after index verification
     *  @param string index
     *  @return chainable
     */
    protected function defaultSetter( $index, $value )
    {
        if (! ($value instanceof $this->object))
            throw new Yz_Exception_InvalidValueType();
        $this->__dict__[$index] = $value;
        return $this;
    }


}